﻿using Microsoft.Extensions.DependencyInjection;
using System;

namespace ServiceCollectionApp
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceCollection services = new ServiceCollection();
            services.AddTransient<ILog, Log>();
            services.AddScoped<IsqlHelper, sqlHelper>();
            //AddScoped,AddTransient,AddSingleton 需要测试

            var provider = services.BuildServiceProvider();

            //依赖注入方法
            var loginfo = provider.GetService<ILog>();
            loginfo.loginfo("This is log");
            var sqlHelper=provider.GetService<IsqlHelper>();
            //sqlHelper.GetAll();

            //传统方法实例化类
            sqlHelper mysql = new sqlHelper();
            mysql.GetAll();

            Console.ReadKey();
        }
    }

    public interface IsqlHelper
    {
        void GetAll();
    }
    public class sqlHelper : IsqlHelper
    {
        //Console.WriteLine("我被构造了一次");
        public sqlHelper()
        {
            Console.WriteLine("我被构造了一次");
        }

        public void GetAll()
        {
            Console.WriteLine("This is 类，name is"+typeof(sqlHelper));
        }
    }

    public interface ILog
    {
        void loginfo(string msg);
    }
    public class Log : ILog
    {
        public void loginfo(string msg)
        {
            Console.WriteLine("This is Mylog"+msg);
        }
    }

}
